<?php
class Grid
{
	private $pageSize = 20;
	private $startPage;
	//	SELECT FirstName, LastName, ABX from abc
	
	function GenrateGrid($sql, $pageNo, $footerPage, $page, $primaryKey, $columns, $add=false, $edit=false, $delete=false, $view=false, $viewPage="", $status=false, $statusField="",$StatusFormat="",$total="")
	{
		$order = "ASC";
		if(isset($_GET['sort']) and isset($_GET['order']))
			if($_GET['order'] == "ASC")
				$order = "DESC";
			else
				$order = "ASC";
		$currentPage = $_GET['page'];
		
		if(isset($_GET['sort']) and (strpos($sql, $_GET['sort']." ASC")or strpos($sql, $_GET['sort']." DESC")))
			$sql .= " ".$_GET['order'];
		elseif(isset($_GET['sort']) and isset($_GET['order']) and strpos($sql,"ORDER BY"))
			$sql .= " , ".$_GET['sort']." ".$_GET['order'];
		elseif(isset($_GET['sort']) and isset($_GET['order']))
			$sql .= " ORDER BY ".$_GET['sort']." ".$_GET['order'];

		//echo $sql;//exit;

		$obj_db = new DB();
		//echo $sql;//exit;
		$obj_db->paginationStart($sql,$this->pageSize,$pageNo);
		//msgbox($obj_db->total_rows());
		//exit;
		$columns = explode(",",$columns);
		$this->startPage = $pageNo;
		$field = "";
		
		if($_SESSION['type'] == "error")
		{
			$table .= '<div align="left"><br />
						<table width="50%" border="0" align="left" cellpadding="0" cellspacing="0" class="errorTable">
						  <tr>
							<td width="8%" align="left" valign="top"><img src="../img/error.png" alt="error" width="29" height="29" /></td>
							<td width="92%" align="left" valign="top" class="error">'.$_SESSION['msg'].'
							</td>
						  </tr>
						</table></div>';
		}
		elseif($_SESSION['type'] == "valid")
		{
			$table .= '<div align="left"><br />
						<table width="50%" border="0" align="left" cellpadding="0" cellspacing="0" class="validTable">
					  <tr>
						<td width="8%" align="left" style="padding-left:10px;" valign="top"><img src="img/icon_confirmation.gif" alt="error" width="29" height="29" /></td>
						<td width="92%" align="left" valign="top" class="valid">'.$_SESSION['msg'].'</td>
					  </tr>
					</table></div>';
		}
		$_SESSION['type']="";
		$_SESSION['msg']="";
		
		if($add)
			$table .= '<div align="right" class="grid"><a href="index.php?page='.$page.'">Add</a></div>';
		
		$table .= '<table class="grid">';
		$table .= "<tr class='gridHeader'>";
		
		foreach($columns as $column)
		{
			
			$info = explode("=",$column);
			
			$table .= "<td align='".$info[0]."' width='".$info[3]."' >
			<strong><a style='color:#000000;' href='index.php?page=".$currentPage."&sort=".$info[2]."&order=".$order."'>".$info[1]."</a></strong></td>";
			$field[] = $info[2];
		}
		
		
		if(!($edit==false and $delete==false and $view==false and $status==false))
			$table .= "<td align='center'><strong>Action</strong></td>";

		$table .= "</tr>";
		
		if($obj_db->total_rows())
		while($row = $obj_db->rsset())
		{
			if((++$i%2)==0)
				$style = "alternateTr";
			else
				$style = "normalTr";
			$table .= '<tr class="'.$style.'">';
			
			$sFormat = explode("=>",$StatusFormat);

			for($j=0; $j<sizeof($field); $j++)
			{
				$display = $row[$field[$j]];
				if($field[$j] == $statusField)
				{
					for($k=0;$k<sizeof($sFormat);$k++)
					{
						$value = explode(",",$sFormat[$k]);
						if($display == $value[0])
						{
							$display = $value[1];
							break;
						}
					}
				}
				$arr = explode("=",$columns[$j]);

				if($arr[sizeof($arr)-1] == "link")
					$display = "<a href='index.php?page=".$footerPage."&id=".$row[$primaryKey]."' >".$display."</a>";
				
				//msgbox ($display);	
				$table .= "<td>".$display."</td>";
			}
			
			if(!($edit==false and $delete==false and $view==false and $status==false))
			{
				$table .= "<td align='center' >";
				
				if($edit)
					$table .= '<a href="index.php?page='.$page.'&action=Update&id='.$row[$primaryKey].'">Edit</a> | ';
				
					
				if($status)
				{
					if($row[$statusField] == 0)
						$display = "Active";
					else
						$display = "Inactive";
	
					$url = "index.php?page=".$page."&action=Status&id=".$row[$primaryKey]."&status=".$row[$statusField];
					
					$table .= "<a href='$url' onClick=\"return confirm('Are your sure to ".strtolower($display)." this?');\">$display</a> | ";
				}	
				if($view)
					$table .= '<a href="index.php?page='.$viewPage.'&action=View&id='.$row[$primaryKey].'&position='.$display.'">View</a> | ';
				
				if($delete)
					$table .= "<a href='index.php?page=".$page."&action=Delete&id=".$row[$primaryKey]."' onClick=\"return confirm('Are you sure, to delete this?');\">Delete</a>";
			}
		
			//if(substr($table, strlen($table)
			
			if (trim(substr($table, strlen($table)-2, strlen($table))) == "|") 
				$table = substr($table, 0, strlen($table)-2);
			
			$table .= "</td>";
			$table .= "</tr>";
		}
		
		$table .= "<tr class='gridFooter'>";
			$colspan = sizeof($field)+1;
		$table .= "<td colspan='".$colspan."'  >"
						.$obj_db->paginationFooter("",$footerPage,$obj_db->num_pages,$obj_db->next_page,$obj_db->prev_page,$this->startPage).
				 	"</td>";
		$table .= "</tr>";
		
		echo $table .= "</table>";
		$obj_db->db_close();
	}
	









function GenrateCGrid($sql, $pageNo, $footerPage, $page, $primaryKey, $columns, $add=false, $edit=false, $delete=false, $view=false, $viewPage="", $status=false, $statusField="",$StatusFormat="",$total="")
	{
		$order = "ASC";
		if(isset($_GET['sort']) and isset($_GET['order']))
			if($_GET['order'] == "ASC")
				$order = "DESC";
			else
				$order = "ASC";
		$currentPage = $_GET['page'];
		
		if(isset($_GET['sort']) and (strpos($sql, $_GET['sort']." ASC")or strpos($sql, $_GET['sort']." DESC")))
			$sql .= " ".$_GET['order'];
		elseif(isset($_GET['sort']) and isset($_GET['order']) and strpos($sql,"ORDER BY"))
			$sql .= " , ".$_GET['sort']." ".$_GET['order'];
		elseif(isset($_GET['sort']) and isset($_GET['order']))
			$sql .= " ORDER BY ".$_GET['sort']." ".$_GET['order'];

		//echo $sql;//exit;

		$obj_db = new DB();
		//echo $sql;exit;
		$obj_db->paginationStart($sql,$this->pageSize,$pageNo);
		//exit;
		$columns = explode(",",$columns);
		$this->startPage = $pageNo;
		$field = "";
		
		if($_SESSION['type'] == "error")
		{
			$table .= '<div align="left"><br />
						<table width="50%" border="0" align="left" cellpadding="0" cellspacing="0" class="errorTable">
						  <tr>
							<td width="8%" align="left" valign="top"><img src="../img/error.png" alt="error" width="29" height="29" /></td>
							<td width="92%" align="left" valign="top" class="error">'.$_SESSION['msg'].'
							</td>
						  </tr>
						</table></div>';
		}
		elseif($_SESSION['type'] == "valid")
		{
			$table .= '<div align="left"><br />
						<table width="50%" border="0" align="left" cellpadding="0" cellspacing="0" class="validTable">
					  <tr>
						<td width="8%" align="left" style="padding-left:10px;" valign="top"><img src="img/icon_confirmation.gif" alt="error" width="29" height="29" /></td>
						<td width="92%" align="left" valign="top" class="valid">'.$_SESSION['msg'].'</td>
					  </tr>
					</table></div>';
		}
		$_SESSION['type']="";
		$_SESSION['msg']="";
		
		if($add)
			$table .= '<div align="right" class="grid"><a href="index.php?page='.$page.'&action=Add">Add</a></div>';
		
		$table .= '<table class="grid">';
		$table .= "<tr class='gridHeader'>";

		foreach($columns as $column)
		{
			$info = explode("=",$column);
			$table .= "<td align='".$info[0]."' width='".$info[3]."' >
			<strong><a style='color:#000000;' href='index.php?page=".$currentPage."&sort=".$info[2]."&order=".$order."'>".$info[1]."</a></strong></td>";
			$field[] = $info[2];
		}
		
		
		if(!($edit==false and $delete==false and $view==false and $status==false))
			$table .= "<td align='center'><strong>Action</strong></td>";

		$table .= "</tr>";
		
		if($obj_db->total_rows())
		while($row = $obj_db->rsset())
		{
			if((++$i%2)==0)
				$style = "alternateTr";
			else
				$style = "normalTr";
			$table .= '<tr class="'.$style.'">';
			
			$sFormat = explode("=>",$StatusFormat);

			for($j=0; $j<sizeof($field); $j++)
			{
			if($field[$j]=="ApprovedByAgent")
			{
				
				if($row[$field[$j]]=="0")
				{
				$display = "<img src='img/reject.jpg'/>";
				}
				elseif($row[$field[$j]]=="1")
				{
				$display = "<img src='img/accept.jpg'/>";
				}
				elseif($row[$field[$j]]=="2")
				{
				$display = "<img  src='img/pending.jpg'/>" ;
				}
				else
				{
				$display = ""; 
				}
				
			}
			elseif($field[$j]=="ApprovedByTEO")
			{
				
				if($row[$field[$j]]=="0")
				{
				$display = "<img src='img/reject.jpg'/>";
				}
				elseif($row[$field[$j]]=="1")
				{
				$display = "<img src='img/accept.jpg'/>";
				}
				elseif($row[$field[$j]]=="2")
				{
				$display = "<img  src='img/pending.jpg'/>" ;
				}
				else
				{
				$display = ""; 
				}
				
			}
			elseif($field[$j]=="ApprovedByManager")
			{

				if($row[$field[$j]]=="0")
				{
				$display = "<img src='img/reject.jpg'/>";
				}
				elseif($row[$field[$j]]=="1")
				{
				$display = "<img src='img/accept.jpg'/>";
				}
				elseif($row[$field[$j]]=="2")
				{
				$display = "<img  src='img/pending.jpg'/>" ;
				}
				else
				{
				$display = ""; 
				}
				
			}
			elseif($field[$j]=="ApprovedBySalesDirector")
			{
				
				if($row[$field[$j]]=="0")
				{
				$display = "<img src='img/reject.jpg'/>";
				}
				elseif($row[$field[$j]]=="1")
				{
				$display = "<img src='img/accept.jpg'/>";
				}
				elseif($row[$field[$j]]=="2")
				{
				$display = "<img  src='img/pending.jpg'/>" ;
				}
				else
				{
				$display = ""; 
				}
				
			}
			elseif($field[$j]=="ApprovedByProjectDirector")
			{
				
				if($row[$field[$j]]=="0")
				{
				$display = "<img src='img/reject.jpg'/>";
				}
				elseif($row[$field[$j]]=="1")
				{
				$display = "<img src='img/accept.jpg'/>";
				}
				elseif($row[$field[$j]]=="2")
				{
				$display = "<img  src='img/pending.jpg'/>" ;
				}
				else
				{
				$display = ""; 
				}
				
			}
			elseif($field[$j]=="ApprovedByCEO")
			{
				
				if($row[$field[$j]]=="0")
				{
				$display = "<img src='img/reject.jpg'/>";
				}
				elseif($row[$field[$j]]=="1")
				{
				$display = "<img src='img/accept.jpg'/>";
				}
				elseif($row[$field[$j]]=="2")
				{
				$display = "<img  src='img/pending.jpg'/>" ;
				}
				else
				{
				$display = ""; 
				}
				
			}
			else
			{
				$display = $row[$field[$j]]; 
				if($field[$j] == $statusField)
				{
					for($k=0;$k<sizeof($sFormat);$k++)
					{
						$value = explode(",",$sFormat[$k]);
						if($display == $value[0])
						{
							$display = $value[1];
							break;
						}
					}
				}
			}
				$table .= "<td>".$display."</td>";
			 
			}
			
			if(!($edit==false and $delete==false and $view==false and $status==false))
			{
				$table .= "<td align='center' >";
				
				if($edit)
					$table .= '<a href="index.php?page='.$page.'&action=Update&id='.$row[$primaryKey].'">Edit</a> | ';
				
					
				if($status)
				{
					if($row[$statusField] == 0)
						$display = "Active";
					else
						$display = "Inactive";
	
					$url = "index.php?page=".$page."&action=Status&id=".$row[$primaryKey]."&status=".$row[$statusField];
					
					$table .= "<a href='$url' onClick=\"return confirm('Are your sure to ".strtolower($display)." this?');\">$display</a> | ";
				}	
				if($view)
					$table .= '<a href="index.php?page='.$viewPage.'&action=View&id='.$row[$primaryKey].'">View</a> | ';
				
				if($delete)
					$table .= "<a href='index.php?page=".$page."&action=Delete&id=".$row[$primaryKey]."' onClick=\"return confirm('Are you sure, to delete this?');\">Delete</a>";
			}
		
			//if(substr($table, strlen($table)
			
			if (trim(substr($table, strlen($table)-2, strlen($table))) == "|") 
				$table = substr($table, 0, strlen($table)-2);
			
			$table .= "</td>";
			$table .= "</tr>";
		}
		
		$table .= "<tr class='gridFooter'>";
			$colspan = sizeof($field)+1;
		$table .= "<td colspan='".$colspan."' >"
						.$obj_db->paginationFooter("",$footerPage,$obj_db->num_pages,$obj_db->next_page,$obj_db->prev_page,$this->startPage).
				 	"</td>";
		$table .= "</tr>";
		
		echo $table .= "</table>";
		$obj_db->db_close();
	}
	
}
?>
